//
//  HLBaseRequest.m
//  YueYue
//
//  Created by hulu001 on 2023/5/17.
//

#import "HLBaseRequest.h"
#import "HLHeaderModel.h"

@implementation HLBaseRequest


#pragma mark ————— 自定义数据 —————

- (NSString *)message {
    if (self.error) {
        return self.error.localizedDescription;
    }
    NSString *message = [NSString stringWithFormat:@"%@",self.result[@"msg"]];
    return message;
}

- (NSString *)code {
    NSString *code;
    if (self.result[@"code"]){
        code = [NSString stringWithFormat:@"%@",self.result[@"code"]];
    } else if (self.result[@"StatusCode"]){
        code = [NSString stringWithFormat:@"%@",self.result[@"StatusCode"]];
    }
    
    return code;
}

- (BOOL)isSuccess {
    
    NSString *code = [self code];
    BOOL isSuccess = NO;
    if ([code isEqualToString:@"200"]) {
        isSuccess = YES;
    }
    
    return  isSuccess;
}

#pragma mark ————— 定义返回数据格式，若是加密要用HTTP接受 —————

- (YTKRequestSerializerType)requestSerializerType
{
    return YTKRequestSerializerTypeJSON;
}

- (YTKResponseSerializerType)responseSerializerType
{
    return YTKResponseSerializerTypeJSON;
}
#pragma mark ————— 默认请求方式 POST —————
-(YTKRequestMethod)requestMethod
{
    return YTKRequestMethodPOST;
}

//#pragma mark ————— 默认请求体是自身转json —————
//-(id)requestArgument{
//    return [self modelToJSONObject];
//}

#pragma mark ————— 请求失败过滤器 —————
-(void)requestFailedFilter
{
    [QMUITips showWithText:[self.error localizedDescription]];
    
    NSLog(@"\nheader:%@\nbody:-%@\nURL:-%@\nResponse:-%@", self.requestHeaderFieldValueDictionary,self.requestArgument,self.requestUrl,self.result);

}

#pragma mark ————— 请求成功过滤器 —————
-(void)requestCompleteFilter
{

    self.result = self.responseJSONObject;
    [QMUIConsole log:self.requestArgument];
    [QMUIConsole log:self.requestUrl];
    [QMUIConsole log:self.result];
    [QMUIConsole log:[NSString stringWithFormat:@"-%@\n-%@\n-%@", self.requestArgument,self.requestUrl,self.result]];
    NSLog(@"\nheader:%@\nbody:-%@\nURL:-%@\nResponse:-%@", self.requestHeaderFieldValueDictionary, self.requestArgument,self.requestUrl,self.result);
    
    NSString *code = [self code];
    if ([code isEqualToString:@"200"]) {
        //请求正常这里不做任何操作
    } else if([code isEqualToString:@"1102"] || [code isEqualToString:@"1100"] || [code isEqualToString:@"9"]){
        NSString *header = [self.requestHeaderFieldValueDictionary mj_JSONString];
        [HLUIHelper showAlertWithContent:[NSString stringWithFormat:@"%@ -- %@ -- %@", code , header, self.requestUrl] callBack:^(BOOL isCom) {
            
        }];
        [HLHelper logout];
        [HLUIHelper setRootLogin];
    } else {
        if (!self.failureCompletionBlock){
            [QMUITips showWithText:self.message];
        }
    }
    
}

#pragma mark ————— 非加密时也要传输的头部内容 也可能不需要，暂时保留 —————
-(NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary{
    //加密header部分
    HLHeaderModel *header = [[HLHeaderModel alloc] init];
    [QMUIConsole log:[header mj_keyValues]];
    return [header mj_keyValues];
    
}

@end
